<template>
  <div id="webgl"></div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import * as THREE from "three";
import earth_img from "../assets/PerspectiveCamera.png";
// 引入gltf模型加载库GLTFLoader.js
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import Cesium_Air from "../assets/Cesium_Air.gltf";

// 创建3D场景对象Scene
const scene = new THREE.Scene();

const shape = new THREE.Shape();
shape.moveTo(10, 0); //.currentPoint变为(10,0)
// 绘制直线线段，起点(10,0)，结束点(100,0)
shape.lineTo(100, 0); //.currentPoint变为(100, 0)
shape.lineTo(100, 100); //.currentPoint变为(100, 100)
shape.lineTo(10, 100); //.currentPoint变为(10, 100)

const geometry = new THREE.ShapeGeometry(shape);
const material = new THREE.MeshLambertMaterial();
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

// 定义threejs输出画布的尺寸(单位:像素px)
const width = window.innerWidth; //宽度
const height = window.innerHeight; //高度
// 实例化一个透视投影相机对象
const camera = new THREE.OrthographicCamera(
  -100 * (width / height),
  100 * (width / height),
  100,
  -100,
  1,
  3000
);
//相机在Three.js三维坐标系中的位置
// 根据需要设置相机位置具体值
camera.position.set(0, 0, 100);
//相机观察目标指向Threejs 3D空间中某个位置
camera.lookAt(0, 0, 0); //指向mesh对应的位置

// AxesHelper：辅助观察的坐标系
const axesHelper = new THREE.AxesHelper(150);
scene.add(axesHelper);

const pointLight = new THREE.PointLight(0xffffff, 1.0);
pointLight.decay = 0.0;
pointLight.position.set(0, 0, 3000);
scene.add(pointLight);
const pointLight2 = new THREE.PointLight(0xffffff, 1.0);
pointLight2.decay = 0.0;
pointLight2.position.set(3000, 0, 0);
scene.add(pointLight2);
const pointLight3 = new THREE.PointLight(0xffffff, 1.0);
pointLight3.decay = 0.0;
pointLight3.position.set(0, 0, -3000);
scene.add(pointLight3);
const pointLight4 = new THREE.PointLight(0xffffff, 1.0);
pointLight4.decay = 0.0;
pointLight4.position.set(-3000, 0, 0);
scene.add(pointLight4);

// 创建渲染器对象
const renderer = new THREE.WebGLRenderer();

renderer.setSize(width, height); //设置three.js渲染区域的尺寸(像素px)
renderer.setClearColor(0xffffff, 1);
renderer.render(scene, camera);

// 设置相机控件轨道控制器OrbitControls
const controls = new OrbitControls(camera, renderer.domElement);
// 如果OrbitControls改变了相机参数，重新调用渲染器渲染三维场景
controls.addEventListener("change", function () {
  renderer.render(scene, camera); //执行渲染操作
}); //监听鼠标、键盘事件

function render() {
  renderer.render(scene, camera); //执行渲染操作
  requestAnimationFrame(render);
}
render();

onMounted(() => {
  document.getElementById("webgl").appendChild(renderer.domElement);
});
</script>
